package com.bdrm.controller.admin;


import com.bdrm.dto.CategoryDTO;
import com.bdrm.dto.CategoryPageQueryDTO;
import com.bdrm.entity.Category;
import com.bdrm.result.PageResult;
import com.bdrm.result.Result;
import com.bdrm.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "菜品分类接口")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation("分类分页查询")
    @PostMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("添加分类")
    @PostMapping("/save")
    public Result save(@RequestBody CategoryDTO categoryDTO){
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @ApiOperation("根据id查询分类")
    @PostMapping("/{id}")
    public Result<Category> getById(@PathVariable Long id){
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    @ApiOperation("修改分类")
    @PutMapping("/update")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @ApiOperation("根据id删除分类")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        System.out.println(id);
        categoryService.deleteById(id);
        return Result.success();
    }

    @ApiOperation("修改分类状态")
    @PostMapping("/{status}/{id}")
    public Result updateStatus(@PathVariable Integer status,@PathVariable Long id){
        categoryService.updateStatus(status, id);
        return Result.success();
    }

    @GetMapping("/list/{type}")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(@PathVariable Integer type){
        log.info("查询分类，type：{}", type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }


}
